package com.sbox.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

public class Utils {
	public static void sleep(int millsec)
	{
		try {	
			Thread.sleep(millsec);
		}catch(Exception e){
			
		}
	}
	
	/**
	 * Check if the album name is reasonable.
	 * Characters not allowed:
	 * 	' ', '/', '\', '|', '?', '*'
	 * @param name
	 * @return
	 */
	private static char[] sIllegalChars = new char[]
			{
				' ', '/', '\\', '|', '?', '*'
			};
	public static boolean checkAlbumName(String name) {
		int len = name.length();
		
		for( int i=0; i<len; i++ )
		{
			char c = name.charAt(i);
			for( int j=0; j<6; j++ )
			{
				if( sIllegalChars[j] == c )
					return false;
			}
		}
		return true;
	}
	
	public static boolean moveFile(String src, String dest) {
		File srcFile = new File(src);
		File destFile = new File(dest);
		FileInputStream fis = null;
		FileOutputStream fos = null;
		byte[] buf = null;
		int len;
		
		if( !srcFile.exists() )
			return true;
		
		try {
			fis = new FileInputStream(srcFile);
			fos = new FileOutputStream(destFile);
			
			buf = new byte[4096];
			
			while(true) {
				len = fis.read(buf);
				
				if( len > 0 ) {
					fos.write(buf);
				}
				if( len <4096 )
					break;
			}
			
			fis.close();
			fos.close();
			return true;
			
		} catch (Throwable e) {
			AlbumLog.error(e.getMessage());
			
			return false;
		}
	}
	
	public static long getDayOfDate(long time) {
		Date d = new Date(time);
		return time;
	}
}
